import { createRouter, createWebHistory } from 'vue-router'
import LotteryGenerator from '../views/LotteryGenerator/index.vue'
import { useUserStore } from '@/stores/user.js'
import { message } from 'ant-design-vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      component: () => import('../views/Home/index.vue')
    },
    {
      path: '/echarts',
      component: () => import('../views/Echarts/index.vue'),
      meta: {
        title: 'Echarts'
      }
    },
    {
      path: '/echarts/:id',
      component: () => import('../views/Echarts/detail/index.vue'),
      name: 'EchartsDetail',
      meta: {
        title: 'Echarts'
      }
    },
    {
      path: '/code-editor',
      name: 'CodeEditor',
      component: () => import('../views/CodeEditor/index.vue'),
      meta: {
        title: '代码编辑器'
      }
    },
    {
      path: '/image-compressor',
      component: () => import('../views/ImageCompressor/index.vue'),
      meta: {
        title: '图片压缩器'
      }
    },
    {
      path: '/git-memo',
      name: 'GitMemo',
      component: () => import('../views/GitMemo/index.vue'),
      meta: {
        title: 'Git 备忘录'
      }
    },
    {
      path: '/qr-code-generator',
      component: () => import('../views/QrCodeGenerator/index.vue'),
      meta: {
        title: '二维码生成器'
      }
    },
    {
      path: '/date-time-converter',
      component: () => import('../views/DateTimeConverter/index.vue'),
      meta: {
        title: '日期时间转换器'
      }
    },
    {
      path: '/timer',
      name: 'Timer',
      component: () => import('../views/Timer/index.vue'),
      meta: {
        title: '计时器'
      }
    },
    {
      path: '/token-generator',
      name: 'TokenGenerator',
      component: () => import('@/views/TokenGenerator/index.vue'),
      meta: {
        title: 'Token生成器'
      }
    },
    {
      path: '/base64-encoder',
      name: 'Base64Encoder',
      component: () => import('../views/Base64Encoder/index.vue'),
      meta: {
        title: 'Base64字符串编码/解码'
      }
    },
    {
      path: '/base64-file-converter',
      component: () => import('../views/Base64FileConverter/index.vue'),
      meta: {
        title: 'Base64文件转换器'
      }
    },
    {
      path: '/color-converter',
      component: () => import('../views/ColorConverter/index.vue'),
      meta: {
        title: '颜色转换器'
      }
    },
    {
      path: '/keycode-info',
      component: () => import('../views/KeycodeInfo/index.vue'),
      meta: {
        title: 'Keycode 信息'
      }
    },
    {
      path: '/vacation',
      component: () => import('../views/Vacation/index.vue'),
      meta: {
        title: '放假安排'
      }
    },
    {
      path: '/lottery-generator',
      name: 'LotteryGenerator',
      component: LotteryGenerator,
      meta: {
        title: '双色球号码生成器'
      }
    },

    {
      path: '/whiteboard',
      component: () => import('../views/WhiteBoard/index.vue'),
      meta: {
        title: '在线白板'
      }
    },
    {
      path: '/xiaobawang',
      component: () => import('../views/XiaoBaWang/index.vue'),
      meta: {
        title: '小霸王游戏机'
      }
    },
    {
      path: '/user-management',
      name: 'UserManagement',
      component: () => import('../views/UserManagement/index.vue'),
      meta: {
        title: '用户管理',
        requiresAdmin: true
      }
    },
    {
      path: '/echarts-generator',
      component: () => import('../views/AddEcharts/index.vue'),
      meta: {
        title: '添加Echarts图表',
        requiresAdmin: true
      }
    },
    {
      path: '/sponsor',
      name: 'Sponsor',
      component: () => import('../views/Sponsor/index.vue'),
      meta: {
        title: '赞助咖啡'
      }
    },
    {
      path: '/coffee-wall',
      name: 'CoffeeWall',
      component: () => import('../views/CoffeeWall/index.vue'),
      meta: {
        title: '咖啡墙'
      }
    },
    {
      path: '/icon-manager',
      redirect: '/font-manager'
    },
    {
      path: '/font-manager',
      name: 'FontManager',
      component: () => import('../views/IconManager/index.vue'),
      meta: {
        title: 'SVG字体管理器'
      }
    },
    {
      path: '/illustration-gallery',
      name: 'IllustrationGallery',
      component: () => import('../views/IllustrationGallery/index.vue'),
      meta: {
        title: '插画分类列表'
      }
    },
    {
      path: '/illustration-gallery/:categoryId',
      name: 'IllustrationDetail',
      component: () => import('../views/IllustrationGallery/detail/index.vue'),
      meta: {
        title: '插画详情'
      }
    },
    {
      path: '/articles',
      name: 'Articles',
      component: () => import('../views/Articles/index.vue'),
      meta: {
        title: '文章列表'
      }
    },
    {
      path: '/articles/write',
      name: 'ArticleWrite',
      component: () => import('../views/Articles/write.vue'),
      meta: {
        title: '写文章',
        requiresAuth: true
      }
    },
    {
      path: '/articles/edit/:id',
      name: 'ArticleEdit',
      component: () => import('../views/Articles/write.vue'),
      meta: {
        title: '编辑文章',
        requiresAuth: true
      }
    },
    {
      path: '/articles/:id',
      name: 'ArticleDetail',
      component: () => import('../views/Articles/detail.vue'),
      meta: {
        title: '文章详情'
      }
    },
    {
      path: '/user',
      name: 'MyProfile',
      component: () => import('../views/User/center.vue'),
      meta: {
        title: '个人主页'
      }
    },
    {
      path: '/user/:id',
      name: 'UserCenter',
      component: () => import('../views/User/center.vue'),
      meta: {
        title: '用户中心'
      }
    },
    {
      path: '/profile/edit',
      name: 'ProfileEdit',
      component: () => import('../views/User/ProfileEdit.vue'),
      meta: {
        title: '编辑个人资料',
        requiresAuth: true
      }
    },
    {
      path: '/file-storage',
      name: 'FileStorage',
      component: () => import('../views/FileStorage/index.vue'),
      meta: {
        title: '文件存储'
      }
    }
  ]
})

// 路由守卫 - 检查权限
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()

  // 检查是否需要登录
  if (to.meta.requiresAuth) {
    if (!userStore.isAuthenticated) {
      message.error('请先登录')
      // 触发登录弹窗
      setTimeout(() => {
        window.dispatchEvent(new CustomEvent('openLogin'))
      }, 100)
      next('/')
      return
    }
  }

  // 检查是否需要管理员权限
  if (to.meta.requiresAdmin) {
    // 检查用户是否已登录
    if (!userStore.isAuthenticated) {
      message.error('请先登录')
      next('/')
      return
    }

    // 检查是否为管理员账号
    if (userStore.user.username !== 'woaichitudouo' && userStore.user.username !== 'woaichitudou') {
      message.error('您没有权限访问此页面')
      next('/')
      return
    }
  }

  next()
})

export default router
